<?php
namespace app\service;

use app\models\Broadcast;
use app\models\Broadcast_group;
use app\models\Staff;

class BroadcastService
{
    /**
     * 添加分组
     * @return bool
     */
    public function insert($data)
    {
        $model = new Broadcast_group();
        return $model->insert($data);
    }

    /**
     * 获取所有分组信息
     * @return array
     */
    public function get_group()
    {
        $model = new Broadcast_group();
        return $model->get_group();
    }

    /**
     * 添加一条广播消息
     * @return bool
     */
    public function insert_broadcast($data)
    {
        $model = new Broadcast();
        return $model->insert($data);
    }

    /**
     * 获取员工所在所有组
     * @param int $job_number
     * @return array
     */
    public function get_staff_group($job_number)
    {
        $model = new Broadcast_group();
        $list = $model->get_group();
        $group = [];
        foreach ($list as $v) {
            $arr = json_decode($v['staff'],true);
            if (in_array($job_number,$arr)) {
                $group[$v['id']] = $v['group_name'];
            }
        }
        return $group;
    }

    /**获取组广播
     * @param array $group
     */
    public function get_broadcast($group)
    {
        $model = new Broadcast();
        $list = $model->get_broadcast_by_group(array_keys($group));
        $staffModel = new Staff();
        $staff_list = $staffModel->staff_list();
        $arr = [];
        foreach ($list as $k=>$v) {
            foreach($staff_list as $v2) {
                if ($v['job_number']==$v2['job_number']) {
                    $v['name'] = $v2['name'];
                }
            }
            $arr[$v['group_id']]['msg'][] = $v;
            $arr[$v['group_id']]['name'] = $group[$v['group_id']];
        }
        return $arr;
    }
}